//
//  SAMemberDetailVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/6/28.
//

#import "SAMemberDetailVC.h"
#import "SAMemberListModel.h"
#import "SAMemberTopUpVC.h"
#import "SAAddMemberVC.h"
#import "SAMemberHistoryVC.h"
#import "SAMemberBalanceVC.h"
#import "SAMemberIntegralVC2.h"
#import "SAMemberDetailAPI.h"
#import "SAVipEditIntegralVC.h"
#import "SAPirntTopUpBillModel.h"
#import "SAPirntTopUpBill.h"

@interface SAMemberDetailVC ()<HTTPAPIDelegate>

@property (nonatomic, strong) NSString *memberId;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UIButton *vipBtn;
@property (nonatomic, strong) SAMemberDetailAPI *memberDetailAPI;
@property (nonatomic, strong) SAMemberListModel *memberModel;

@property (nonatomic, strong) UILabel *historyLab;
@property (nonatomic, strong) UILabel *balanceLab;
@property (nonatomic, strong) UILabel *integralLab;

@property (nonatomic, strong) SAPirntTopUpBillModel *billModel;
@property (nonatomic, strong) SAOverallSetting *overallSetting;

@end

@implementation SAMemberDetailVC

-(instancetype)initWithMemberId:(NSString *)memberId {
    if (self = [super init]) {
        self.memberId = memberId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.memberDetailAPI == api) {
        self.memberModel = [SAMemberListModel mj_objectWithKeyValues:data[@"data"]];
        self.nameLab.text = self.memberModel.memberName;
        [self.vipBtn setTitle:[NSString stringWithFormat:@"  %@",self.memberModel.memberLevelStr] forState:UIControlStateNormal];
        self.historyLab.text = [NSString stringWithFormat:@"%0.2lf",[self.memberModel.addupConsumeAmount doubleValue]/100];
        self.balanceLab.text = [NSString stringWithFormat:@"%0.2lf",[self.memberModel.totalAmount doubleValue]/100];
        self.integralLab.text = self.memberModel.totalPoints;
        
        if (self.billModel != nil && self.billModel.isPrint == YES) {
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            NSString *timeFormatStr = [dateFormatter stringFromDate:[NSDate date]];
            
            SAPirntTopUpBillModel *printModel = [SAPirntTopUpBillModel new];
            printModel.employees = self.billModel.employees;
            printModel.topUp = self.billModel.topUp;
            printModel.giveAway = self.billModel.giveAway;
            printModel.settlementType = self.billModel.settlementType;
            printModel.date = timeFormatStr;
            printModel.vipName = self.memberModel.memberName;
            printModel.phone = self.memberModel.mobile;
            printModel.balance = self.balanceLab.text;
            printModel.integral = self.integralLab.text;
            //打印充值小票
            if (self.overallSetting.printerSetting.printerStatus == 2 && [SAValueManger shareInStance].printType == 1) {
                [SAPirntTopUpBill printWithTotalData:printModel printSuccessBlock:^{

                }];;
            }else {
                [Prompt popPromptViewWithMsg:@"未连接打印机" duration:2];
            }
            self.billModel = nil;
        }
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {

}

#pragma mark - event
-(void)clickedTopUpBtn {
    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
    NSString *catalogMemberRecharge = [NSString stringWithFormat:@"%@",menuList[6][@"perms"][@"catalogMemberRecharge"]];
    if ([catalogMemberRecharge isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有充值权限,请联系店长添加" duration:2];
        return;
    }
    
    SAMemberTopUpVC *pushVC = [[SAMemberTopUpVC alloc] initWithMemberId:self.memberModel.memberId MemberName:self.memberModel.memberName AndTitle:self.memberModel.totalAmount];
    WeakSelf
    pushVC.refreshBlock = ^(SAPirntTopUpBillModel * _Nonnull billMode) {
        !weakSelf.refreshBlock ?: weakSelf.refreshBlock();
        weakSelf.billModel = billMode;
        [weakSelf.memberDetailAPI sendRequestToServerWithActivityViewAndMask:GET];
    };
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedBasicBtn {
    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
    NSString *catalogMemberUpdate = [NSString stringWithFormat:@"%@",menuList[6][@"perms"][@"catalogMemberUpdate"]];
    if ([catalogMemberUpdate isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有编辑会员权限,请联系店长添加" duration:2];
        return;
    }
    
    SAAddMemberVC *pushVC = [SAAddMemberVC new];
    pushVC.model = self.memberModel;
    WeakSelf
    pushVC.refreshBlock = ^{
        !self.refreshBlock ?: self.refreshBlock();
        [weakSelf.memberDetailAPI sendRequestToServerWithActivityViewAndMask:GET];
    };
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedHistoryBtn {
    SAMemberHistoryVC *pushVC = [[SAMemberHistoryVC alloc] initWithMemberId:self.memberModel.memberId];
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedBalanceBtn {
    SAMemberBalanceVC *pushVC = [[SAMemberBalanceVC alloc] initWithMemberId:self.memberModel.memberId AndTitle:self.memberModel.totalAmount];
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedIntegralBtn {
    SAMemberIntegralVC2 *pushVC = [[SAMemberIntegralVC2 alloc] initWithMemberId:self.memberModel.memberId AndTitle:self.memberModel.totalPoints];
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedEditBtn {
    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
    NSString *modifyPoints = [NSString stringWithFormat:@"%@",menuList[6][@"perms"][@"modifyPoints"]];
    if ([modifyPoints isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有修改积分权限,请联系店长添加" duration:2];
        return;
    }
    
    SAVipEditIntegralVC *pushVC = [[SAVipEditIntegralVC alloc] initWithMemberId:self.memberModel.memberId AndIntegral:self.memberModel.totalPoints];
    WeakSelf
    pushVC.refreshBlock = ^{
        [weakSelf.memberDetailAPI sendRequestToServerWithActivityViewAndMask:GET];
    };
    [self.navigationController pushViewController:pushVC animated:YES];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"会员详情"];
    self.view.backgroundColor = kBgColor;
    self.overallSetting = [SAOverallSetting shareInstance];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 5), kScreenWidth, kScaleNum(80))];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:SARectMake(26, 21, 38, 38)];
    imageV.image = [UIImage imageNamed:@"user_headerImage"];
    imageV.contentMode = UIViewContentModeScaleAspectFit;
    [whiteView addSubview:imageV];
    
    UILabel *nameLab = [[UILabel alloc] initWithFrame:SARectMake(85, 20, 150, 20)];
    nameLab.textColor = kBlackColor;
    nameLab.font = kFont(13);
    nameLab.textAlignment = NSTextAlignmentLeft;
    [whiteView addSubview:nameLab];
    self.nameLab = nameLab;
    
    UIButton *vipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    vipBtn.frame = SARectMake(85, 40, 150, 20);
    [vipBtn setImage:[UIImage imageNamed:@"vip_level"] forState:UIControlStateNormal];
    [vipBtn setTitleColor:kLightgGrayColor forState:UIControlStateNormal];
    vipBtn.titleLabel.font = kFont(13);
    vipBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [whiteView addSubview:vipBtn];
    self.vipBtn = vipBtn;
    
    UIButton *topUpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    topUpBtn.frame = SARectMake(kBaseScreenWidth - 80, 27, 65, 36);
    [topUpBtn setTitle:@"充值" forState:UIControlStateNormal];
    [topUpBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    topUpBtn.titleLabel.font = kFont(15);
    topUpBtn.backgroundColor = kPinkColor;
    topUpBtn.layer.cornerRadius = kScaleNum(18);
    topUpBtn.layer.masksToBounds = YES;
    [topUpBtn addTarget:self action:@selector(clickedTopUpBtn) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:topUpBtn];
    
    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    editBtn.frame = SARectMake(kBaseScreenWidth - 175, 27, 85, 36);
    [editBtn setTitle:@"修改积分" forState:UIControlStateNormal];
    [editBtn setTitleColor:kPinkColor forState:UIControlStateNormal];
    editBtn.backgroundColor = [UIColor whiteColor];
    editBtn.layer.cornerRadius = kScaleNum(18);
    editBtn.layer.masksToBounds = YES;
    editBtn.layer.borderWidth = kScaleNum(1);
    editBtn.layer.borderColor = kPinkColor.CGColor;
    editBtn.titleLabel.font = kFont(15);
    [editBtn addTarget:self action:@selector(clickedEditBtn) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:editBtn];
    
    UIView *whiteView2 = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 90), kScreenWidth, kScaleNum(163))];
    whiteView2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView2];
    
    NSArray *arr = @[@"基本信息", @"历史消费", @"余额明细", @"积分明细"];
    
    for (int i = 0; i < arr.count; i++) {
        UILabel *nameLab = [[UILabel alloc] initWithFrame:SARectMake(26, 41 * i, 120, 40)];
        nameLab.textColor = kBlackColor;
        nameLab.font = kFont(15);
        nameLab.text = arr[i];
        nameLab.textAlignment = NSTextAlignmentLeft;
        [whiteView2 addSubview:nameLab];
        
        UILabel *contentLab = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 182, 41 * i, 150, 40)];
        contentLab.textColor = kBlackColor;
        contentLab.font = kFont(15);
        contentLab.textAlignment = NSTextAlignmentRight;
        contentLab.text = @"";
        [whiteView2 addSubview:contentLab];
        if (i == 1) {
            self.historyLab = contentLab;
        }else if (i == 2) {
            self.balanceLab = contentLab;
        }else if (i == 3) {
            self.integralLab = contentLab;
        }
        
        UIImageView *allowImageV = [[UIImageView alloc] initWithFrame:SARectMake(355, 13 + 41 * i, 7, 13)];
        allowImageV.image = [UIImage imageNamed:@"shop_grayAllow"];
        [whiteView2 addSubview:allowImageV];
        
        if (i != (arr.count - 1)) {
            UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 40 + 41 * i, kBaseScreenWidth, 1)];
            line.backgroundColor = kBgColor;
            [whiteView2 addSubview:line];
        }
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = SARectMake(0, 41 * i, kBaseScreenWidth, 40);
        [whiteView2 addSubview:btn];
        if (i == 0) {
            [btn addTarget:self action:@selector(clickedBasicBtn) forControlEvents:UIControlEventTouchUpInside];
        }else if (i == 1) {
            [btn addTarget:self action:@selector(clickedHistoryBtn) forControlEvents:UIControlEventTouchUpInside];
        }else if (i == 2) {
            [btn addTarget:self action:@selector(clickedBalanceBtn) forControlEvents:UIControlEventTouchUpInside];
        }else if (i == 3) {
            [btn addTarget:self action:@selector(clickedIntegralBtn) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    self.memberDetailAPI.memberId = self.memberId;
    [self.memberDetailAPI sendRequestToServerWithActivityViewAndMask:GET];
}

-(SAMemberDetailAPI *)memberDetailAPI {
    if (!_memberDetailAPI) {
        _memberDetailAPI = [SAMemberDetailAPI apiWithDelegate:self];
    }
    return _memberDetailAPI;
}

@end
